<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the fetchImages method from the BingImagesSearchEnhancement class, for the Dart programming language.">
  <title>fetchImages method - BingImagesSearchEnhancement class - creator library - Dart API</title>


  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" rel="stylesheet">
  
  <link rel="stylesheet" href="../../static-assets/github.css?v1">
  <link rel="stylesheet" href="../../static-assets/styles.css?v1">
  <link rel="icon" href="../../static-assets/favicon.png?v1">

  
</head>


<body data-base-href="../../" data-using-base-href="false" class="light-theme">

<div id="overlay-under-drawer"></div>

<header id="title">
  <span id="sidenav-left-toggle" class="material-symbols-outlined" role="button" tabindex="0">menu</span>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="../../index.html">yuuna</a></li>
    <li><a href="../../creator/creator-library.html">creator</a></li>
    <li><a href="../../creator/BingImagesSearchEnhancement-class.html">BingImagesSearchEnhancement</a></li>
    <li class="self-crumb">fetchImages method</li>
  </ol>
  <div class="self-name">fetchImages</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
  <div class="toggle" id="theme-button">
    <label for="theme">
      <input type="checkbox" id="theme" value="light-theme">
      <span id="dark-theme-button" class="material-symbols-outlined">
        brightness_4
      </span>
      <span id="light-theme-button" class="material-symbols-outlined">
        brightness_5
      </span>
    </label>
  </div>
</header>
<main>

  <div id="dartdoc-main-content" class="main-content">
      <div>
<h1><span class="kind-method">fetchImages</span> method 
</h1></div>

    <section class="multi-line-signature">
      
  <div>
    <ol class="annotation-list">
        <li>@<a href="https://api.flutter.dev/flutter/dart-core/override-constant.html">override</a></li>
    </ol>
  </div>

<span class="returntype"><a href="https://api.flutter.dev/flutter/dart-async/Future-class.html">Future</a><span class="signature">&lt;<wbr><span class="type-parameter"><a href="https://api.flutter.dev/flutter/dart-core/List-class.html">List</a><span class="signature">&lt;<wbr><span class="type-parameter"><a href="https://pub.dev/documentation/network_to_file_image/4.0.1/network_to_file_image/NetworkToFileImage-class.html">NetworkToFileImage</a></span>&gt;</span></span>&gt;</span></span>
<span class="name ">fetchImages</span>(<wbr><ol class="parameter-list"><li><span class="parameter" id="fetchImages-param-appModel">{<span>required</span> <span class="type-annotation"><a href="../../models/AppModel-class.html">AppModel</a></span> <span class="parameter-name">appModel</span>, </span></li>
<li><span class="parameter" id="fetchImages-param-context"><span>required</span> <span class="type-annotation"><a href="https://api.flutter.dev/flutter/widgets/BuildContext-class.html">BuildContext</a></span> <span class="parameter-name">context</span>, </span></li>
<li><span class="parameter" id="fetchImages-param-searchTerm"><span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/String-class.html">String</a>?</span> <span class="parameter-name">searchTerm</span>}</span></li>
</ol>)

      <div class="features"><span class="feature">override</span></div>

    </section>
    
<section class="desc markdown">
  <p>Given a search term, generate a list of images.</p>
</section>


    
<section class="summary source-code" id="source">
  <h2><span>Implementation</span></h2>
  <pre class="language-dart"><code class="language-dart">@override
Future&lt;List&lt;NetworkToFileImage&gt;&gt; fetchImages({
  required AppModel appModel,
  required BuildContext context,
  String? searchTerm,
}) async {
  if (_bingCache[searchTerm!] != null) {
    return _bingCache[searchTerm]!;
  }

  List&lt;NetworkToFileImage&gt; images = [];

  bool webViewBusy = true;

  HeadlessInAppWebView webView = HeadlessInAppWebView(
      initialUrlRequest: URLRequest(
        url: Uri.parse(
          &quot;https:&#47;&#47;www.bing.com&#47;images&#47;search?q=$searchTerm&#39;)&quot;,
        ),
      ),
      initialOptions: InAppWebViewGroupOptions(
        android: AndroidInAppWebViewOptions(
          blockNetworkImage: true,
        ),
      ),
      onLoadStop: (controller, uri) async {
        Directory appDirDoc = await getApplicationSupportDirectory();
        String bingImagesPath = &#39;${appDirDoc.path}&#47;bingImages&#39;;
        Directory bingImagesDir = Directory(bingImagesPath);
        if (bingImagesDir.existsSync()) {
          if (_bingCache.isEmpty) {
            bingImagesDir.deleteSync(recursive: true);
          }
        }
        bingImagesDir.createSync();

        await Future.delayed(const Duration(milliseconds: 1000), () {});

        dom.Document document = parser.parse(await controller.getHtml());

        List&lt;dom.Element&gt; imgElements =
            document.getElementsByClassName(&#39;iusc&#39;);

        String timestamp =
            DateFormat(&#39;yyyyMMddTkkmmss&#39;).format(DateTime.now());
        Directory imageDir = Directory(&#39;$bingImagesPath&#47;$timestamp&#39;);
        imageDir.createSync();
        for (int i = 0; i &lt; imgElements.length; i++) {
          Map&lt;dynamic, dynamic&gt; imgMap =
              jsonDecode(imgElements[i].attributes[&#39;m&#39;]!);
          String imageURL = imgMap[&#39;turl&#39;];

          String imagePath = &#39;${imageDir.path}&#47;$i&#39;;
          File imageFile = File(imagePath);

          &#47;&#47;&#47; Instant export requires a file to already be written to the
          &#47;&#47;&#47; file system.
          if (i == 0) {
            File networkFile =
                await DefaultCacheManager().getSingleFile(imageURL);
            networkFile.copySync(imagePath);
          }
          NetworkToFileImage image = NetworkToFileImage(
            url: imageURL,
            file: imageFile,
          );

          images.add(image);
        }

        webViewBusy = false;
      });

  await webView.run();

  while (webViewBusy) {
    await Future.delayed(const Duration(milliseconds: 100), () {});
  }

  if (images.isNotEmpty) {
    _bingCache[searchTerm] = images;
  }

  return images;
}</code></pre>
</section>


  </div> <!-- /.main-content -->

  <div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left">
    <header id="header-search-sidebar" class="hidden-l">
  <form class="search-sidebar" role="search">
    <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
  <li><a href="../../index.html">yuuna</a></li>
  <li><a href="../../creator/creator-library.html">creator</a></li>
  <li><a href="../../creator/BingImagesSearchEnhancement-class.html">BingImagesSearchEnhancement</a></li>
  <li class="self-crumb">fetchImages method</li>
</ol>


    <h5>BingImagesSearchEnhancement class</h5>
    <ol>

        <li class="section-title"><a href="../../creator/BingImagesSearchEnhancement-class.html#constructors">Constructors</a></li>
          <li><a href="../../creator/BingImagesSearchEnhancement/BingImagesSearchEnhancement.html">BingImagesSearchEnhancement</a></li>


        <li class="section-title inherited">
          <a href="../../creator/BingImagesSearchEnhancement-class.html#instance-properties">Properties</a>
        </li>
          <li class="inherited"><a href="../../creator/Enhancement/description.html">description</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/descriptionLocalisation.html">descriptionLocalisation</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/field.html">field</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/hashCode.html">hashCode</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/icon.html">icon</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/label.html">label</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/labelLocalisation.html">labelLocalisation</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/runtimeType.html">runtimeType</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/uniqueKey.html">uniqueKey</a></li>

        <li class="section-title"><a href="../../creator/BingImagesSearchEnhancement-class.html#instance-methods">Methods</a></li>
          <li><a href="../../creator/BingImagesSearchEnhancement/enhanceCreatorParams.html">enhanceCreatorParams</a></li>
          <li><a href="../../creator/BingImagesSearchEnhancement/fetchImages.html">fetchImages</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/getLocalisedDescription.html">getLocalisedDescription</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/getLocalisedLabel.html">getLocalisedLabel</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/initialise.html">initialise</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/noSuchMethod.html">noSuchMethod</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/prepareResources.html">prepareResources</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/toString.html">toString</a></li>

        <li class="section-title inherited"><a href="../../creator/BingImagesSearchEnhancement-class.html#operators">Operators</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/operator_equals.html">operator ==</a></li>




        <li class="section-title"><a href="../../creator/BingImagesSearchEnhancement-class.html#constants">Constants</a></li>
          <li><a href="../../creator/BingImagesSearchEnhancement/key-constant.html">key</a></li>
</ol>

  </div><!--/.sidebar-offcanvas-->

  <div id="dartdoc-sidebar-right" class="sidebar sidebar-offcanvas-right">
  </div><!--/.sidebar-offcanvas-->

</main>

<footer>
  <span class="no-break">
    yuuna
      2.8.0+93
  </span>

  
</footer>



<script src="../../static-assets/highlight.pack.js?v1"></script>
<script src="../../static-assets/docs.dart.js"></script>



</body>

</html>

